package cn.tedu.store.product.webapi.controller;

import cn.tedu.store.commons.pojo.product.dto.SpuAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.SpuUpdateDTO;
import cn.tedu.store.commons.pojo.product.vo.SpuListItemVO;
import cn.tedu.store.commons.pojo.product.vo.SpuStandardVO;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.product.service.ISpuService;
import cn.tedu.store.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品SPU控制器
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "09.SPU管理模块")
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @ApiOperation("添加SPU")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated SpuAddNewDTO SpuAddNewDTO) {
        log.debug("开始处理[添加SPU]的请求: 参数:{}", SpuAddNewDTO);
        spuService.addNew(SpuAddNewDTO);
        log.debug("添加spu成功!");
        return JsonResult.ok();
    }

    @ApiOperation("删除SPU")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "SpuId", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理[删除SPU]的请求 参数:{}", id);
        spuService.deleteById(id);
        log.debug("删除SPU成功!");
        return JsonResult.ok();
    }

    @ApiOperation("商品上架")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/published")
    public JsonResult<Void> setPublished(@PathVariable Long id) {
        log.debug("准备处理[商品上架]请求 参数:{}", id);
        spuService.setPublished(id);
        return JsonResult.ok();
    }

    @ApiOperation("商品下架")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/not-published")
    public JsonResult<Void> setNotPublished(@PathVariable Long id) {
        log.debug("准备处理[商品下架]请求 参数:{}", id);
        spuService.setNotPublished(id);
        return JsonResult.ok();
    }

    @ApiOperation("新品推荐")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/new-arrival")
    public JsonResult<Void> setNewArrival(@PathVariable Long id) {
        log.debug("准备处理[新品推荐]请求 参数:{}", id);
        spuService.setNewArrival(id);
        return JsonResult.ok();
    }

    @ApiOperation("取消新品推荐")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/not-new-arrival")
    public JsonResult<Void> setNotNewArrival(@PathVariable Long id) {
        log.debug("准备处理[取消新品推荐]请求 参数:{}", id);
        spuService.setNotNewArrival(id);
        return JsonResult.ok();
    }

    @ApiOperation("推荐商品")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/recommend")
    public JsonResult<Void> setRecommend(@PathVariable Long id) {
        log.debug("准备处理[推荐商品]请求 参数:{}", id);
        spuService.setRecommend(id);
        return JsonResult.ok();
    }

    @ApiOperation("取消推荐商品")
    @ApiOperationSupport(order = 350)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/unrecommend")
    public JsonResult<Void> setNotRecommend(@PathVariable Long id) {
        log.debug("准备处理[取消推荐商品]请求 参数:{}", id);
        spuService.setNotRecommend(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改SPU")
    @ApiOperationSupport(order = 360)
    @ApiImplicitParam(name = "id", value = "SPUid", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id , SpuUpdateDTO spuUpdateDTO) {
        log.debug("处理[修改SPU]请求 参数:{},{}", id, spuUpdateDTO);
        spuService.update(id, spuUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("获取SPU详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "SPUid", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/get-by-id")
    public JsonResult<SpuStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理[获取SPU详情]请求 参数:{}",id);
        return JsonResult.ok(spuService.getStandardById(id));
    }

    @ApiOperation("SPU列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int")
    })
    @GetMapping("")
    public JsonResult<JsonPage<SpuListItemVO>> list(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize)
    {
        log.debug("开始处理[获取SPU列表]请求 参数:{}{}",pageNum, pageSize);
        return JsonResult.ok(spuService.list(pageNum, pageSize));
    }
}
